package xnj.lazydog.btcontroller.ControlViews.ControlViews;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;

import java.text.DecimalFormat;

import xnj.lazydog.btcontroller.ControlViews.ViewOptions;
import xnj.lazydog.btcontroller.R;

public class ControlText extends ControlElement {



    float TEXT_SIZE_RATIO = 0.5f;

    DecimalFormat decimalFormat;
    public ControlText(ViewOptions options)
    {
        super(options);

        decimalFormat = new DecimalFormat("##.####");
        p.setTextAlign(Paint.Align.CENTER);
        p.setColor(THEME_TEXT_DARK_COLOR);
        settingsAfterLink = false;
    }

    RectF rect = new RectF();

    @Override
    public void onSizeChanged() {
        text_height_pix = h_pix*TEXT_SIZE_RATIO;
        if(Math.abs(text_height_pix-last_text_height_pix)>5)
        {
            p.setTextSize(text_height_pix);
            last_text_height_pix = text_height_pix;
        }
        text_y_pix = text_height_pix*0.4f;

        rect.left = -w_pix*0.5f;
        rect.right = w_pix*0.5f;
        rect.top = -h_pix*0.5f;
        rect.bottom = h_pix*0.5f;

    }

    @Override
    public void draw(Canvas canvas) {

        canvas.save();
        canvas.clipRect(rect);

        if(linkedValues[0]==null) {
            canvas.drawText("文本框",0,text_y_pix,p);
        }else
        {
            canvas.drawText(valuesName[0]+" : "+decimalFormat.format(linkedValues[0].get()),0,text_y_pix,p);
        }
        canvas.restore();
    }

    @Override
    public void clicked(Context context) {
    }
    @Override
    public void dragged() {

    }

    @Override
    public void released() {

    }

    @Override
    public void pressed() {

    }

    @Override
    public void zoomed(float sx, float sy) {

    }

    @Override
    public void showSettingsDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(R.layout.toy_settings_text);

        AlertDialog dialog = builder.create();

        if(dialog.getWindow()!=null)
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        dialog.show();

    }
}
